import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import DBSCAN

dataS = np.random.rand(100, 2)
plt.scatter(dataS[:,0],dataS[:,1])
plt.show()

#建立模型，其中eps代表距离阀值，min_samples代表核心对象在eps领域的样本数阀值
#dbscan = DBSCAN(eps=1,min_samples=4)
dbscan = DBSCAN(eps=1.5,min_samples=5)
dbscan.fit(dataS)


result = dbscan.fit_predict(dataS)
print(result)

#画图
plt.rcParams['font.sans-serif']=['SimHei']#用来正常显中文标签，SimHei代表黑体
plt.rcParams['axes.unicode_minus']=False#用来正常显示负号，minus代表负号
plt.scatter(dataS[result==-1,0],dataS[result==-1,1],s=150,c='purple',marker='8',label='噪声点')


plt.scatter(dataS[result==0,0],dataS[result==0,1],s=150,c='orange',marker='o',label='cluster=1')
plt.scatter(dataS[result==1,0],dataS[result==1,1],s=150,c='green',marker='s',label='cluster=2')
plt.scatter(dataS[result==2,0],dataS[result==2,1],s=150,c='blue', marker='^',label='cluster=3')
plt.scatter(dataS[result==3,0],dataS[result==3,1],s=150,c='red',marker='*',label='cluster=4')
plt.scatter(dataS[result==4,0],dataS[result==4,1],s=150,c='black',marker='p',label='cluster=5')

#p代表五边形



plt.legend(loc='lower left',bbox_to_anchor=(-0.4,0.5,0.1,0.1))
plt.show()
